package com.leetCode;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-23 01:49:52
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-23     张李鑫                     1.0         1.0 Version
 */
public class FindMedianSortedArrays {
    /**
     * 给定两个大小分别为 m 和 n 的正序（从小到大）数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数
     */

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n1L = nums1.length;
        int n2L = nums2.length;
        int[] arr = new int[n1L+n2L];
        int n1Index = 0;
        int n2Index = 0;
        int index = 0;

        while (n1Index < n1L && n2Index < n2L) {
            arr[index++] = nums1[n1Index] < nums2[n2Index] ? nums1[n1Index++] : nums2[n2Index++];
        }
        while (n1Index<n1L){
            arr[index++]=nums1[n1Index++];
        }
        while (n2Index<n2L){
            arr[index++]=nums2[n2Index++];
        }
        if (arr.length%2!=0){
            return arr[arr.length/2];
        }
        return (double) (arr[arr.length/2]+arr[arr.length/2-1])/2;
    }

    public static void main(String[] args) {
        int []a={1,2};
        int []b={3,4};
        double medianSortedArrays = findMedianSortedArrays(a, b);
        System.out.println(medianSortedArrays);
    }
}
